home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / norm.pro < prev    next >
Text File  |  1997-07-08  |  3KB  |  85 lines

  1. ;$Id: norm.pro,v 1.9 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       NORM
  8. ;
  9. ; PURPOSE:
  10. ;       1) This function computes the Euclidean norm of a vector.
  11. ;          OR
  12. ;       2) This function computes the Infinity norm of an array.
  13. ;
  14. ; CATEGORY:
  15. ;       Complex Linear Algebra.
  16. ;
  17. ; CALLING SEQUENCE:
  18. ;       Result = NORM(A)
  19. ;
  20. ; INPUTS:
  21. ;       A:      An N-element real or complex vector.
  22. ;               An M by N real or complex array.
  23. ;
  24. ; KEYWORD PARAMETERS:
  25. ;       DOUBLE: If set to a non-zero value, computations are done in
  26. ;               double precision arithmetic.
  27. ;
  28. ; EXAMPLE:
  29. ;       1) Define an N-element complex vector (a).
  30. ;            a = [complex(1, 0), complex(2,-2), complex(-3,1)]
  31. ;          Compute the Euclidean norm of (a).
  32. ;            result = norm(a)
  33. ;
  34. ;       2) Define an M by N complex array (a). 
  35. ;            a = [[complex(1, 0), complex(2,-2), complex(-3,1)], $
  36. ;                 [complex(1,-2), complex(2, 2), complex(1, 0)]]
  37. ;          Compute the Infinity norm of the complex array (a).
  38. ;            result = norm(a)
  39. ;
  40. ; PROCEDURE:
  41. ;       NORM.PRO computes the Euclidean norm of an N-element vector.
  42. ;       NORM.PRO computes the Infinity norm of an M by N array
  43. ;
  44. ; REFERENCE:
  45. ;       ADVANCED ENGINEERING MATHEMATICS (seventh edition)
  46. ;       Erwin Kreyszig
  47. ;       ISBN 0-471-55380-8
  48. ;
  49. ; MODIFICATION HISTORY:
  50. ;       Written by:  GGS, RSI, April 1992
  51. ;       Modified:    GGS, RSI, February 1994
  52. ;                    Computes the Euclidean norm of an N-element vector.
  53. ;                    Accepts complex inputs. Added DOUBLE keyword.
  54. ;       Modified:    GGS, RSI, September 1994
  55. ;                    Added support for double-precision complex inputs.
  56. ;       Modified:    GGS, RSI, April 1996
  57. ;                    Modified keyword checking and use of double precision.
  58. ;-
  59.  
  60. FUNCTION Norm, A, Double = Double
  61.   
  62.   ON_ERROR, 2  ;Return to caller if error occurs.
  63.  
  64.   Type = SIZE(A) 
  65.  
  66.   if N_ELEMENTS(Double) eq 0 then $
  67.     Double = (Type[Type[0]+1] eq 5) or $
  68.              (Type[Type[0]+1] eq 9)
  69.  
  70.   ;TOTAL(DoubleData, Double = 0) returns a double-precision result. Cast the
  71.   ;result to FLOAT if Double = 0.
  72.   if Type[0] eq 1 then begin ;If vector, compute the Euclidean norm.
  73.     if Double eq 0 then RETURN, $
  74.       FLOAT(SQRT(TOTAL(ABS(A)^2, Double = Double))) $ ;ABS needed for complex. 
  75.     else RETURN, SQRT(TOTAL(ABS(A)^2, Double = Double))
  76.   endif else $
  77.   if Type[0] eq 2 then begin ;If array, compute the Infinity norm.
  78.     if Double eq 0 then RETURN, $
  79.       FLOAT(MAX(TOTAL(ABS(A), 1, Double = Double))) $
  80.     else RETURN, MAX(TOTAL(ABS(A), 1, Double = Double))
  81.   endif else MESSAGE, $
  82.     "Input must be an N-element vector or an M by N array."
  83.  
  84. END
  85.